<template>
  <div class="login-page">
    <!-- 顶部标题 -->
    <van-nav-bar title="面经登录" />

    <!-- 表单 -->
  <van-form @submit="onSubmit">
  <van-field
    v-model="username"
    name="username"
    label="用户名"
    placeholder="用户名"
    :rules="[{ required: true, message: '请填写用户名' }]"
  />
  <van-field
    v-model="password"
    type="password"
    name="password"
    label="密码"
    placeholder="密码"
    :rules="[{ required: true, message: '请填写密码' }]"
  />
  <div style="margin: 16px;">
    <van-button  block type="info" native-type="submit">提交</van-button>
  </div>
</van-form>

<!-- 超链接 -->
 <router-link  class="link" to="/register">还没有账号，去注册</router-link>
  </div>
</template>

<script>
// 按需导入API方法
import { loginAPI } from '@/api/user'

export default {
  name: 'login-page',
  data () {
    return {
      username: '', // 双向绑定
      password: '' // 和密码双向绑定
    }
  },
  methods: {
    async onSubmit (values) {
      // values 就是账号和密码
      // console.log('submit', values)
      const { data: res } = await loginAPI(values)
      // console.log(res)
      this.$toast.success('登录成功') // 提示
      localStorage.setItem('mobile-token', res.data.token) // 存储token
      this.$router.push('/article') // 跳转到面经列表页
    }
  }
}
</script>

<style lang="less" scoped>
.link{
  font-size: 12px;
  color: #1989fa;
  float: right;
  margin-right: 20px;
}
</style>
